Skip to content

Bmp: streaming row reader and streaming encoder session - #19

Merged
sdcb merged 4 commits into
mainfrom
streaming/bmp
Nov 23, 2026
Merged

sdcb merged 4 commits into
mainfrom
streaming/bmp

Conversation

@sdcb

@sdcb sdcb commented Nov 23, 2026

Copy link
Copy Markdown
Owner

What

BMP gets true forward-only streaming read and streaming write.

Decode. BmpImageDecoder now implements IStreamingImageDecoder and sets Registration.CreateRowReader. The reader is BmpRowReader : StreamingRowReader. It reuses the span decoder's row kernels (BmpPixels.PlanRows / DecodeRow were factored out of DecodeUncompressed), so its rows equal TryDecodeInto's byte for byte. Its Info equals TryReadInfo's, and the header is parsed with the same BmpHeader.Parse in a new prefix mode.

Bitmap Non-seekable stream Seekable stream (or retainConsumed input)
Uncompressed (BI_RGB / BI_BITFIELDS / BI_ALPHABITFIELDS, every depth and palette), top-down streams streams
The same, bottom-up fallback streams: one forward pass to the end of the pixels, then a seek back to each ~256 KiB band of stored rows, handed out last stored row first
32-bit BI_RGB read with alpha (the "alpha unless all 0" rule) fallback streams: the forward pass ORs every fourth byte first (AlphaOr, Vector with a byte-at-a-time head and tail)
RLE, Huffman 1D, BI_PNG, non-basic-8 formats, non-full options, offBits into the headers or > 1 MiB, V5 profile after the pixels fallback fallback

The bottom-up forward pass exists because StreamInput.Rewind only goes back. Reaching the last band means reading up to it, so the pixels are read twice. Abstractions need: a forward seek on StreamInput (a Rewind that accepts a position past Position when the stream can seek) would remove that pass for bottom-up files without the alpha scan.

Encode. BmpImageEncoder.Create returns a StreamingEncoderSession when TopDown is set, Format is an explicit direct-colour layout (Rgb555, Rgb565, Rgb24, Rgb32, Argb32) and Rle is off. The headers go out at AddFrame, rows go out as they are written, and the profile goes out at Commit. Header building was factored into BmpEncoder.PlanFile / WriteHeaders, which Write also uses, so the bytes equal the buffered session's under any chunking or stride. Everything else keeps today's buffered session.

Tests (BmpStreamingTests)

  • Rows equal TryDecodeInto for 13 layouts × top-down / bottom-up × 7 formats. Each case runs the seekable Whole feed (expects streaming) and the OneByte / RandomChunks / AsyncOnly feeds (expect topDown && !alphaAmbiguous). Info matches too.
  • Bands: multi-band bottom-up files read through a seekable stream that returns short reads.
  • Alpha-scan decision, last row missing its padding, retained input, and every fallback case.
  • Truncation throws InvalidDataException under every feed.
  • AlphaOr is checked against a byte-at-a-time loop.
  • Memory: an 8192×4096 decode allocates < 8 MB, both top-down from a chunked non-seekable stream and bottom-up 32-bit from a MemoryStream. A streaming encode of the same size allocates < 4 MB.
  • Encoder: bytes equal BmpEncoder.Encode (what the buffered session runs) for 6 layouts × 7 input formats, both whole and in random bands with padded strides. EncoderEmitsBeforeCommit passes, and the non-streaming options report IsStreaming == false.
  • End to end: a Magick.NET bottom-up BMP read from a FileStream streams, and from an async-only ChunkedStream it falls back. Both give the same rows as TryDecodeInto. A top-down file round-trips through the streaming encoder and a FileStream reader.
  • Streaming fuzz soak: BMP_STREAM_FUZZ_ITERATIONS=300000 … -method Sdcb.SimdCodecs.Tests.BmpStreamingTests.Nothing_but_invalid_data_from_malformed_streams passes. It checks that streaming and TryDecodeInto agree on success and rows, and that nothing but InvalidDataException is thrown. BMP_FUZZ_ITERATIONS=300000 … BmpRobustness also passes.
  • Full suite: 7074 tests, 0 failed. The 559 skips are oracle tools missing on this machine for other codecs. The BMP classes pass with REQUIRE_ORACLES=1, and also with DOTNET_EnableAVX2=0 and DOTNET_EnableHWIntrinsic=0. No new warnings.
  • Deliberately breaking the alpha decision or the AlphaOr offset makes the tests fail.

Benchmark (AMD Ryzen 7 5800X, Windows 11 26200, .NET 10.0.15, AVX2, 1920×1080, shared machine)

New contestants: current rows ← stream (the reader over a MemoryStream, 64 rows per call), current ← read whole stream (read the stream into a buffer, then TryDecodeInto), and current session → writer. New encode cases: rgb24-topdown and argb32-topdown. The run is recorded in results/streaming-2026-11-23-amd-ryzen-7-5800x.md.

  • Span decode, current ÷ baseline (533b03e): 1.03× photo, 1.00× screen (geomean over 14 cases, within noise). A per-row call into the factored-out row decoder cost about 25% on 256×256 icons, so DecodeRow is now AggressiveInlining. BDN after the fix: 7.95 µs vs 7.87 µs.
  • Streaming reader ÷ span decode on bytes already in memory: 1.46× / 1.51×. This misses the ≤ 1.05× target. These layouts decode at close to memcpy speed, and the reader has to copy every byte into its buffer (twice for bottom-up), so the copy dominates. Palettes are 1.02 to 1.12×.
  • Streaming reader ÷ read-whole-then-decode (what streaming replaces): 0.91× / 0.93×.
  • Encoder session ÷ Encode(…, Stream) on the top-down cases: 0.69 to 0.90×.

Notes for the coordinator

  • AGENTS.md soak list: add BMP_STREAM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~BmpStreaming".
  • Abstractions: forward seek on StreamInput (see above).
  • Not merged.

🤖 Generated with Claude Code

sdcb and others added 4 commits November 23, 2026 19:37
BmpImageDecoder implements IStreamingImageDecoder. Uncompressed bitmaps
(BI_RGB, BI_BITFIELDS, BI_ALPHABITFIELDS, every depth and palette) stream
to the 8-bit basic formats with the span decoder's row kernels: top-down
from any stream; bottom-up from a seekable (or retained) input by one
forward pass, then a seek back per ~256 KiB band of stored rows. A 32-bit
BI_RGB bitmap read with alpha scans its fourth bytes in that forward pass
(seekable only). RLE, Huffman 1D, BI_PNG, other formats or options, a
pixel offset into the headers, and a V5 profile after the pixels fall
back. Rows equal TryDecodeInto's, Info equals TryReadInfo's.

BmpImageEncoder.Create returns a StreamingEncoderSession for top-down,
explicit direct-colour, non-RLE options: headers at AddFrame, rows as
they arrive, the profile at Commit; bytes equal the buffered session's.

The benchmark gains a streaming-reader decode contestant, an encoder
session contestant and two top-down encode cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ead-whole-stream path

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The per-row call cost ~25% on 256x256 icon images (BDN, 5800X).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sdcb
sdcb merged commit 35b35f9 into main Nov 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant